//package com.lyt.springboot.Interceptor;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.lyt.springboot.annotion.RequestLimit;
//import com.lyt.springboot.util.IpUtil;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.servlet.ServletContext;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//import java.util.concurrent.TimeUnit;
//
//@Component
//public class RequestLimitInterceptor implements HandlerInterceptor {
//    private final Logger log = LoggerFactory.getLogger(this.getClass());
//
//    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        try {
//            if (handler instanceof HandlerMethod) {
//
//                //
//                System.out.println("拦截器开始");
//
//                String serverName = request.getServerName();
//                int serverPort = request.getServerPort();
//                HttpSession session = request.getSession();
//                ServletContext servletContext = request.getServletContext();
//                System.out.println("serverName是"+serverName);
//                System.out.println("serverPort是"+serverPort);
//                System.out.println("session是"+session);
//                System.out.println("servletContext是"+servletContext);
//                String method = request.getMethod();
//                String contextPath = request.getContextPath();
//                String url = request.getRequestURL().toString();
//                String servletPath = request.getServletPath();
//                System.out.println("method是"+method);
//                System.out.println("contextPath是"+contextPath);
//                System.out.println("url是"+url);
//                System.out.println("servletPath是"+servletPath);
//
////                HandlerMethod handlerMethod = (HandlerMethod) handler;
////                // 获取RequestLimit注解
////                RequestLimit requestLimit = handlerMethod.getMethodAnnotation(RequestLimit.class);
////                if (null==requestLimit) {
////                    return true;
////                }
////                //限制的时间范围
////                int seconds = requestLimit.second();
////                //时间内的 最大次数
////                int maxCount = requestLimit.maxCount();
//                String ipAddr = IpUtil.getIpAddr(request);
//                // 存储key
//                String key =  ipAddr+":"+request.getContextPath() + ":" + request.getServletPath();
//                // 已经访问的次数
//                Integer count = (Integer) redisTemplate.opsForValue().get(key);
//                log.info("检测到目前ip={} 对接口:{} 已经访问的次数:{}",ipAddr, request.getServletPath() , count);
//                if (null == count || -1 == count) {
//                    redisTemplate.opsForValue().set(key, 1, 60, TimeUnit.SECONDS);
//                    return true;
//                }
//                if (count < 4) {
//                    redisTemplate.opsForValue().increment(key);
//                    return true;
//                }
//                log.info("请求过于频繁请稍后再试");
//                returnData(response);
//                return false;
//            }
//            return true;
//        } catch (Exception e) {
//            log.warn("请求过于频繁请稍后再试");
//            e.printStackTrace();
//        }
//        return true;
//    }
//
//    public void returnData(HttpServletResponse response) throws IOException {
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
//        ObjectMapper objectMapper = new ObjectMapper();
//        //这里传提示语可以改成自己项目的返回数据封装的类
//        response.getWriter().println(objectMapper.writeValueAsString("请求过于频繁请稍后再试"));
//        return;
//    }
//
//}
